﻿using System;
using System.Collections.Specialized;
using System.Configuration;

namespace Webbness.Mvc.Config
{
    public class ApplicationSettings
    {
        private static bool? _disableSecurity;
        private static NameValueCollection settingsDict;
        private static string _CCEmailsTo;

        public static bool DisableSecurity
        {
            get
            {
                ensureAppSettings();
                if (_disableSecurity == null)
                {
                    var keyValueConfigurationElement = settingsDict["DisableSecurity"];
                    _disableSecurity = keyValueConfigurationElement == null ? false : Convert.ToBoolean(keyValueConfigurationElement);
                }
                return _disableSecurity.Value;
            }
        }

        public static string CCEmailsTo
        {
            get
            {
                ensureAppSettings();
                if (_CCEmailsTo == null)
                {
                    _CCEmailsTo = settingsDict["CCEmailsTo"];
                }
                return _CCEmailsTo;
            }
        }

        private static void ensureAppSettings()
        {
            if (settingsDict != null)
            {
                return;
            }
            settingsDict = System.Web.Configuration.WebConfigurationManager.AppSettings;
            if (0 == settingsDict.Count)
            {
                throw new ConfigurationErrorsException("'appSettings not found in web.config.");
            }
        }
    }
}